package com.dllo.lanou.cookapp.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dllo.lanou.cookapp.BaseApplication;
import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.activity.OneKeyLoginActivty;
import com.dllo.lanou.cookapp.entity.User;
import com.dllo.lanou.cookapp.utils.CutPictureUtil;

import java.io.File;
import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * Created by xijinhua on 2016/3/5.
 */
public class PersonInfoFragment extends Fragment implements View.OnClickListener {
    @Bind(R.id.loginorregister)
    Button loginOrRegister;
    @Bind(R.id.Linear)
    LinearLayout linearLayout;
    @Bind(R.id.fl_layout)
    FrameLayout frameLayout;
    @Bind(R.id.iv_backgrand)
    ImageView iv_backgrand;

    @Bind(R.id.tv_getname)
    TextView tv_getname;
    @Bind(R.id.iv_icon)
    ImageView iv_icon;
    User userInfo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, null);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        iv_backgrand.setOnClickListener(this);
        iv_icon.setOnClickListener(this);
        tv_getname.setOnClickListener(this);
        userInfo = BmobUser.getCurrentUser(getActivity(), User.class);
        loginOrRegister.setOnClickListener(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (userInfo != null) {
            // 允许用户使用应用
            Log.i("String", "我已经在利用" + "");
            linearLayout.setVisibility(View.GONE);
            frameLayout.setVisibility(View.VISIBLE);

            if (userInfo != null) {

                tv_getname.setText(userInfo.getUsername());
                if (userInfo.getImg() != null) {
                    Log.i("String", "我已经在利用" + userInfo.getImg());
                    userInfo.getImg().loadImage(getActivity(), iv_icon);
                }
                if (userInfo.getBackground_img() != null) {
                    userInfo.getBackground_img().loadImage(getActivity(), iv_backgrand);
                }
            }
        } else {
            //缓存用户对象为空时， 可打开用户注册界面…
            linearLayout.setVisibility(View.VISIBLE);
            frameLayout.setVisibility(View.GONE);
        }


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginorregister:
                Intent intent = new Intent(getActivity(), OneKeyLoginActivty.class);
                startActivityForResult(intent, 2);
                break;
            case R.id.iv_icon:
                //系统图片
                Intent intent_icon = new Intent(Intent.ACTION_PICK, null);
                intent_icon.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent_icon, 12);
                break;

            case R.id.tv_getname:

                break;

            case R.id.iv_backgrand:
                //系统图片
                Intent intent_bc = new Intent(Intent.ACTION_PICK, null);
                intent_bc.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent_bc,14);
                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            if (BaseApplication.isLogin() == true) {
                linearLayout.setVisibility(View.GONE);
                frameLayout.setVisibility(View.VISIBLE);
              //  User user = BaseApplication.instance.getUser();
            userInfo = BmobUser.getCurrentUser(getActivity(), User.class);
                Log.i("woqu","--"+userInfo);
                Log.i("woqu","--"+userInfo.getUsername());
                Log.i("woqu","--"+userInfo.getImg());

                if (userInfo != null) {
                    tv_getname.setText(userInfo.getUsername());
                   // Log.i("测试信息",userInfo2.getUsername()+"大大大"+userInfo.getImg());
                    Log.i("woqu","--"+userInfo.getImg());
                    if (userInfo.getImg() != null) {
                    userInfo.getImg().loadImage(getActivity(), iv_icon);
                }
                    if (userInfo.getBackground_img() != null) {
                   userInfo.getBackground_img().loadImage(getActivity(), iv_backgrand);
                }
                }

            }
        }
        if (requestCode == 12) {
            Intent intent =CutPictureUtil.getInstance(getActivity()).getImageView(data.getData());
            startActivityForResult(intent, 13);
        }
        if (requestCode == 13) {
            if (data != null) {

              String url =  CutPictureUtil.getInstance(getActivity()).setPicToView(data,iv_icon);
                //修改云端服务器的User表,将路径传到云服务器上
              final  BmobFile bmobFile = new BmobFile(new File(url));
                bmobFile.upload(getActivity(), new UploadFileListener() {
                    @Override
                    public void onSuccess() {
                        User newuser = new User();
                        newuser.setImg(bmobFile);
                        Log.i("newusr", newuser + "");
                        Log.i("测试6", userInfo + "");
                       // CutPictureUtil.getInstance(getActivity()).updateBmobFile(newuser,userInfo);
                        newuser.update(getActivity(), userInfo.getObjectId(), new UpdateListener() {
                            @Override
                            public void onSuccess() {
                                //
                                Log.i("更新用户", "更新用户成功");
                            }

                            @Override
                            public void onFailure(int i, String s) {

                            }
                        });
                    }

                    @Override
                    public void onFailure(int i, String s) {

                    }
                });

            }

        }

        if (requestCode == 14)
        {
            if (data != null) {
                Intent intent =CutPictureUtil.getInstance(getActivity()).getImageView(data.getData());
                startActivityForResult(intent, 15);
            }
        }

        if (requestCode == 15){
            if (data != null) {
                String url =  CutPictureUtil.getInstance(getActivity()).setPicToView(data,iv_backgrand);
                //修改云端服务器的User表,将路径传到云服务器上
                final  BmobFile bmobFile = new BmobFile(new File(url));
                bmobFile.upload(getActivity(), new UploadFileListener() {
                    @Override
                    public void onSuccess() {
                        User newuser = new User();
                        newuser.setBackground_img(bmobFile);
                        Log.i("newusr", newuser + "");
                        Log.i("测试6",userInfo+"");
                        // CutPictureUtil.getInstance(getActivity()).updateBmobFile(newuser,userInfo);
                        newuser.update(getActivity(), userInfo.getObjectId(), new UpdateListener() {
                            @Override
                            public void onSuccess() {
                                //
                                Log.i("更新用户", "更新用户成功");
                            }

                            @Override
                            public void onFailure(int i, String s) {

                            }
                        });
                    }

                    @Override
                    public void onFailure(int i, String s) {

                    }
                });

            }

        }

    }



}
